﻿Imports System.Data.OleDb
Public Class u_ThucDon

    Private Sub LoadData()
        Dim td As New ThucDonBUS
        DgvThucDon.DataSource = td.GetThuDon
    End Sub

    Private Sub u_ThucDon_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
        LoadData()
        cbxDonVi.Items.Add("Cái")
        cbxDonVi.Items.Add("Đĩa")
        cbxDonVi.Items.Add("Tô")
        cbxDonVi.Items.Add("Chai")
        cbxDonVi.Items.Add("Ly")
        cbxDonVi.SelectedIndex = 0
        DSTD1 = ThucDonBUS.Get_DSTD()
        DgvThucDon.DataSource = DSTD1
    End Sub


    Private Sub btnThemTD_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThemTD.Click
        Dim newTD As New ThucDonDTO
        newTD.MaMonAn = txtMaMonAn.Text
        newTD.TenMonAn = txtTenMonAn.Text
        newTD.DonVi = cbxDonVi.SelectedItem.ToString
        newTD.TinhTrang = rbCon.Checked
        newTD.Gia = Integer.Parse(txtGiaTien.Text.ToString)
        Dim tdBUS As New ThucDonBUS
        tdBUS.AddThucDon(newTD)
        LoadData()
    End Sub

    Private Sub DgvThucDon_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DgvThucDon.SelectionChanged
        If DgvThucDon.SelectedRows.Count <> 0 Then
            If DgvThucDon.SelectedRows(0).Index <> DgvThucDon.RowCount Then
                txtMaMonAn.Text = DgvThucDon.SelectedRows(0).Cells("MaMonAn").Value
                txtTenMonAn.Text = DgvThucDon.SelectedRows(0).Cells("TenMonAn").Value
                txtGiaTien.Text = DgvThucDon.SelectedRows(0).Cells("Gia").Value.ToString
                cbxDonVi.SelectedValue = DgvThucDon.SelectedRows(0).Cells("DonVi").Value
                If DgvThucDon.SelectedRows(0).Cells("TinhTrang").Value = True Then
                    rbCon.Checked = True
                Else
                    rbHet.Checked = True
                End If
                txtxuatsu.Text = DgvThucDon.SelectedRows(0).Cells("XuatXu").Value
            End If
        End If
    End Sub

    Private Sub btnXoaTD_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoaTD.Click
        If DgvThucDon.SelectedRows.Count <> 0 Then
            If DgvThucDon.SelectedRows(0).Index <> DgvThucDon.RowCount Then
                Dim delTD As New ThucDonDTO
                delTD.STT = DgvThucDon.SelectedRows(0).Cells("STT").Value
                Dim tdBUS As New ThucDonBUS
                tdBUS.DelThucDon(delTD)
                LoadData()
            End If
        End If
    End Sub

    Private Sub btnCapNhatTD_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCapNhatTD.Click
        If DgvThucDon.SelectedRows.Count <> 0 Then
            If DgvThucDon.SelectedRows(0).Index <> DgvThucDon.RowCount Then
                Dim newTD As New ThucDonDTO
                newTD.STT = DgvThucDon.SelectedRows(0).Cells("STT").Value
                newTD.MaMonAn = txtMaMonAn.Text
                newTD.TenMonAn = txtTenMonAn.Text
                newTD.DonVi = cbxDonVi.SelectedItem.ToString
                newTD.XuatSu = txtxuatsu.Text
                newTD.Gia = txtGiaTien.Text
                newTD.TinhTrang = rbCon.Checked
                Dim tdBUS As New ThucDonBUS
                tdBUS.UpdateThucDon(newTD)
                LoadData()
            End If
        End If
    End Sub

    Dim DSTD1 As DataTable = New DataTable()
    Private Sub btnXuat_Click(sender As Object, e As EventArgs) Handles btnXuat.Click
        Dim SaveFileDialog1 As SaveFileDialog = New SaveFileDialog()
        SaveFileDialog1.Filter = "Export (*.xls)|*.xls"
        SaveFileDialog1.ShowDialog()
        Dim File_name As String = SaveFileDialog1.FileName
        ExcelProvider.file_name = File_name
        Try

            For i As Integer = 0 To DSTD1.Rows.Count - 1
                Dim sql As String
                sql = String.Format("INSERT INTO [Sheet1$] (STT,MaMonAn,TenMonAn,DonVi,Gia,TinhTrang,XuatXu) values ('{0}', '{1}','{2}','{3}', '{4}','{5}','{6}')", DSTD1.Rows(i)(0), DSTD1.Rows(i)(1), DSTD1.Rows(i)(2), DSTD1.Rows(i)(3), DSTD1.Rows(i)(4), DSTD1.Rows(i)(5), DSTD1.Rows(i)(6))
                ExcelProvider.NonQuery(sql)
            Next
            MessageBox.Show("Xuất dữ liệu thành công")
        Catch ex As Exception
            MessageBox.Show("Xuất dữ liệu bị lỗi, bạn vui lòng kiểm tra lại cấu trúc file .xls")
        End Try
    End Sub
End Class
